package NIOCommunication;

import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;

public abstract class NIOContext
{
	protected NIOProcessor mNIOProcessor;
	private static int mGlobalContextID = 0;
	private int mContextID;

	protected NIOContext(NIOProcessor processor)
	{
		mNIOProcessor = processor;
		mContextID = mGlobalContextID++;
	}
	
	public int GetContextID()
	{
		return mContextID;
	}
	
	protected void PerformActions(SelectionKey key)
	{	
		if(key.isReadable())
		{
			//System.out.println("OnReadable");
			OnReadable(key);
		}
		
		if(key.isWritable())
		{
			//System.out.println("OnWriteable");
			OnWritable(key);
		}
		
		if(key.isConnectable())
		{
			//System.out.println("OnConnectable");
			OnConnectable(key);
		}
		
		if(key.isAcceptable())
		{
			//System.out.println("OnAcceptable");
			OnAcceptable(key);
		}
	}

	public void OnReadable(SelectionKey key)
	{
		System.out.println("Base NIOContext OnReadable().");
	}
	
	public void OnWritable(SelectionKey key)
	{
		System.out.println("Base NIOContext OnWriteable().");
	}
	
	public void OnConnectable(SelectionKey key)
	{
		System.out.println("Base NIOContext OnConnectable().");
	}
	
	public void OnAcceptable(SelectionKey key)
	{
		System.out.println("Base NIOContext OnAcceptable().");
	}
	
	public void Close()
	{
		System.out.println("Base NIOContext OnClose().");
	}
        
        public void OnTimeout()
        {
            System.out.println("Base NIOContext OnTimeout().");
        }
	
	public abstract SelectableChannel GetChannel();
}
